package com.ihome.android;

import java.io.File;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Application;
import android.os.Handler;

import com.ihome.android.common.ConfigUtil;
import com.ihome.android.market2.info.AppInfo;
import com.ihome.android.market2.info.HomepagecontentEdu;
import com.ihome.android.market2.info.TemplateContentInfo;
import com.ihome.android.market2.info.TopTitleEdu;
import com.ihome.android.netstatus.EANetChangeObserver;
import com.ihome.android.netstatus.EANetWorkUtil.netType;
import com.ihome.android.netstatus.EANetworkStateReceiver;
import com.ihome.android.util.AppUtils;
import com.ihome.android.util.EAInjector;
import com.ihome.android.util.Util;
import com.ihome.android.util.exception.EAAppException;
import com.ihome.android.util.log.LogThread;
import com.ihome.android.util.log.LogUtil;

public class EAApp extends Application {
	private static final String							TAG					= "EAApp";
	/** 加载类注入器 */
	private EAInjector									mInjector;
	/** App异常崩溃处理器 */
	private UncaughtExceptionHandler					uncaughtExceptionHandler;
	private static EAApp								EAApp;
	private EAActivity									currentActivity;
	/** ThinkAndroid 应用程序运行Activity管理器 */
	private ActivityMgr									mAppManager;
	private Boolean										networkAvailable	= false;
	private EANetChangeObserver							eaNetChangeObserver;

	public static ThreadGroup							uploadThreadGroup	= new ThreadGroup("uploadThreadGroup");
	public static boolean								isSystemApp			= false;

	public String										cacheKey			= "";
	/**
	 * 首页Title数据
	 */
	public List<TemplateContentInfo>					mCategoryList		= new ArrayList<TemplateContentInfo>();
	/**
	 * 首页推荐位数据
	 */
	public HashMap<Integer, List<TemplateContentInfo>>	mRecommList			= new HashMap<Integer, List<TemplateContentInfo>>();
	/**
	 * 需要升级的APP List
	 */
	public List<AppInfo>								mUpdateAppList		= new ArrayList<AppInfo>();

	/**
	 * 文件下载的Handler
	 */
	public List<Handler>								mDLHandler			= new ArrayList<Handler>();
	
	/**
	 * 首页的导航页面数据 Edu
	 */
	public List<TopTitleEdu>					mTopTitleEduList		= new ArrayList<TopTitleEdu>();
	/**
	 * 首页的数据 Edu
	 */
	public List<HomepagecontentEdu>					mHomeContentEduList		= new ArrayList<HomepagecontentEdu>();


	@Override
	public void onCreate() {

		onPreCreateApplication();
		super.onCreate();
		doOncreate();
		onAfterCreateApplication();
		getActivityMgr();

		isSystemApp = AppUtils.isSystemApp(getPackageName());
		LogUtil.d(TAG, "is SystemApp : " + isSystemApp);

	}

	private void doOncreate() {

		this.EAApp = this;
		// 注册App异常崩溃处理器
		//		setUncaughtExceptionHandler(CrashHandler.getInstance(this));
		//		Thread.setDefaultUncaughtExceptionHandler(getUncaughtExceptionHandler());
		//		eaNetChangeObserver = new EANetChangeObserver() {
		//			@Override
		//			public void onConnect(netType type) {
		//
		//				super.onConnect(type);
		//				EAApplication.this.onConnect(type);
		//			}
		//
		//			@Override
		//			public void onDisConnect() {
		//
		//				super.onDisConnect();
		//				EAApplication.this.onDisConnect();
		//
		//			}
		//		};
		EANetworkStateReceiver.registerObserver(eaNetChangeObserver);
		
		//日志记录模块
		//		boolean isDebug = true;
		//		if (isDebug) {
		//			int pId = android.os.Process.myPid();
		//			File file = new File(ConfigUtil.LOG_PATH, Util.createTimeStamp() + "_Market.log");
		//			new LogThread("" + pId, file).start();
		//		}
	}

	/**
	 * 当前没有网络连接
	 */
	public void onDisConnect() {
		networkAvailable = false;
		if (currentActivity != null && currentActivity.getIsRunning()) {
			currentActivity.onDisConnect();
		}
	}

	/**
	 * 网络连接连接时调用
	 */
	protected void onConnect(netType type) {

		networkAvailable = true;
		if (currentActivity != null) {
			currentActivity.onConnect(type);
		}
	}

	/**
	 * 获取Application
	 * 
	 * @return
	 */
	public static EAApp getApp() {
		return EAApp;
	}

	protected void onAfterCreateApplication() {}

	protected void onPreCreateApplication() {
		ConfigUtil.InitPath(getApplicationContext());
	}

	/**
	 * 设置 App异常崩溃处理器
	 * 
	 * @param uncaughtExceptionHandler
	 */
	public void setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) {
		this.uncaughtExceptionHandler = uncaughtExceptionHandler;
	}

	private UncaughtExceptionHandler getUncaughtExceptionHandler() {
		if (uncaughtExceptionHandler == null) {
			uncaughtExceptionHandler = EAAppException.getInstance(this);
		}
		return uncaughtExceptionHandler;
	}

	public EAInjector getInjector() {
		if (mInjector == null) {
			mInjector = EAInjector.getInstance();
		}
		return mInjector;
	}

	public void setInjector(EAInjector injector) {
		this.mInjector = injector;
	}

	public void onActivityCreating(EAActivity activity) {

	}

	public void onActivityCreated(EAActivity activity) {
		currentActivity = activity;
	}

	public ActivityMgr getActivityMgr() {
		if (mAppManager == null) {
			mAppManager = ActivityMgr.getInstance();
		}
		return mAppManager;
	}

	/**
	 * 退出应用程序
	 * 
	 * @param isBackground
	 *            是否开开启后台运行,如果为true则为后台运行
	 */
	public void exitApp(Boolean isBackground) {
		mAppManager.AppExit(this, isBackground);
	}

	/**
	 * 获取当前网络状态，true为网络连接成功，否则网络连接失败
	 * 
	 * @return
	 */
	public Boolean isNetworkAvailable() {
		return networkAvailable;
	}

	public enum NavigationDirection {
	Forward, Backward
	}

	public void addHandler(Handler handler) {
		LogUtil.v(TAG, "addHandler() ---> Before mDLHandler.size = " + mDLHandler.size());
		if (!mDLHandler.contains(handler)) {
			mDLHandler.add(handler);
			LogUtil.v(TAG, "addHandler() ---> After mDLHandler.size = " + mDLHandler.size());
		}
	}

	public void clearHandler(Handler handler) {
		LogUtil.v(TAG, "clearHandler() ---> Before mDLHandler.size = " + mDLHandler.size());
		if (mDLHandler.contains(handler)) {
			mDLHandler.remove(handler);
			LogUtil.v(TAG, "clearHandler() ---> After mDLHandler.size = " + mDLHandler.size());
		}
	}
	
	public List<TemplateContentInfo> getRecommListByID(int sectionID){
		return mRecommList.get(sectionID);
	} 
}
